// index.js
 
import Vue from 'vue';
import Vuex from 'vuex';
import {setSotrage,getStorage} from "@/utils/util.js"
import { getUsual } from '@/core/web-api/common';
import {getMyInfo} from "@/core/web-api/qiuzhiduan.js"
import {getMyCompanyInfo} from "@/core/web-api/qiyeduan.js"
Vue.use(Vuex);
 // 1待审核0审核通过2审核拒绝3草稿默认草稿
const store =  new Vuex.Store({
	state: {
		systemInfo:{},	// 机型信息
		menuRectInfo:{},	// 小程序胶囊信息
		openId:"",
		wechat:"",
		baseImage:"https://api.zhifengkeji.cn/uploads/",
		uploadUrl:"/file/uploadBase64",
		type:"",
		token:"",
	//	type:"1",	// type:1为企业 2为求职者
	//	token:"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJndWxpLXVzZXIiLCJpYXQiOjE3MjY3MTMxODEsImlkIjoxNywibmlja25hbWUiOiIxODgxMDMwMDExOSIsInJvbGUiOiJDT01QQU5ZIn0.Hn4GCPFnFPWisfCU5MQS5H8icLRn48wMvb1qcEHVwXyokeZx1ziBWVSs9exZtCzUhLok6ENks_5mcviTqmEb8A",
	//	type:"2",
	//	token:"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJndWxpLXVzZXIiLCJpYXQiOjE3MjY3MTI3MDYsImlkIjoxOCwibmlja25hbWUiOiIxNzgwMTQyMDExOSIsInJvbGUiOiJTRUVLRVIifQ.3WEhPZqZoXTAcy-d8hCgGDV0KuUDAwbbd5XqAAh5UI1YKGn3z3EU2-r23v6eh6nNX27NZGx0MguI5QUwhmQC5Q",
		indexFilters:{},	// 筛选
		userInfo:{},
		usual:{},
		imgUrl:"https://api.zhifengkeji.cn/uploads/static/",
	  	socketUrl:"wss://api.zhifengkeji.cn/websocket/",
	 // socketUrl:"ws://192.168.1.14:7015/chongqingzhaopin/websocket/",
		messageList:[], 
		isMessage:false,
		youkeType:"",
		sessId:"",
		isRead:"",
	},
	mutations: {
		setYoukeType(state,val){ 
			state.youkeType = val;
		},
		setYkqiyeToken(state,val){ 
			state.ykqiyeToken = val;
		},
		setYkseekerToken(state,val){ 
			state.ykseekerToken = val;
		},
		setIsMessage(state,val){
			state.isMessage = val;
		},
		/**
		 * 设置当前会话
		 * @param {Object} state
		 * @param {Object} val
		 */
		setSessId(state,val){
			state.sessId = val || "";
		},
		/**
		 * 清楚消息列表消息
		 * @param {Object} state
		 */
		clearImList(state){
			state.messageList = [];
		},
		/**
		 * 消息列表新增消息
		 * @param {Object} state
		 * @param {Object} item
		 */
		setImList(state,item){
			state.messageList.push(item);
		},
		clearIsRead(state){
			state.isRead = "";
		},
		setIsRead(state,item){
			state.isRead=1;
		},
		/**
		 * 系统配置
		 * @param {Object} state
		 * @param {Object} info
		 */
		setUsual(state,info){
			state.usual = info;
		},
		/**
		 * 获取个人信息
		 * @param {Object} state 
		 * @param {Object} info
		 */
		setUserInfo(state,info){
			state.userInfo = info;
		},
		// 设置筛选字段
		setFilters(state,obj){
			state.indexFilters = obj;
		},
		// 设置角色
		setType(state,value){
			state.type = value;
		},
		// 设置token
		setToken(state,value){
			if(value){
				state.youkeType = ""
			}
			state.token = value;
		},
		// 赋值机型信息
		setSystemInfo(state,info){
			state.systemInfo = info || uni.getSystemInfoAsync();
		},
		// 赋值胶囊信息
		setMenuRectInfo(state,info){
			state.menuRectInfo = info || uni.getMenuButtonBoundingClientRect();
		},
		setOpenId(state,id){
			state.openId = id || "";
		},
		setWechar(state,value){
			state.wechat = value || ""
		}
	},
	actions: {
		getUsualAction(state){
			getUsual().then(res=>{
				if(res.code==20000){
					state.commit("setUsual",res.data.usual);
				}
			})
		},
		
		// 异步加载个人信息
		getUserInfoAction(state){
			let getUserInfo = null
			if(state.state.type=='1'){
				getUserInfo = getMyCompanyInfo();
			}else if(state.state.type=='2'){
				getUserInfo = getMyInfo()
			}
			if(!getUserInfo) return;
			getUserInfo.then(res=>{
				if(res.code==20000){
					state.commit("setUserInfo",res.data.detail);
				}
			})
		}
	},
	getters: {
		// 获取状态栏高度
		getStateHeight(state){
			return state.systemInfo? state.systemInfo.statusBarHeight:0;
		},
		// 获取胶囊高度
		getMenuRectHeight(state){
			return state.systemInfo? state.menuRectInfo.height : 0;
		},
		// 获取状态栏和胶囊高度
		getStateAndMenuRectHeight(state){
			return state.systemInfo? (state.menuRectInfo.bottom + state.menuRectInfo.top - state.systemInfo.statusBarHeight):0;
		},
		// 获取胶囊和标题高度
		getNavbar(state){
			return  state.systemInfo? ((state.menuRectInfo.top - state.systemInfo.statusBarHeight) * 2 + state.menuRectInfo.height):0
		},
		// 获取底部安全区域
		getSafeBottom(state){
			return state.systemInfo? ((state.systemInfo.screenHeight - state.systemInfo.safeArea.bottom) || 20):0
		},
		getPageHeight(state){
			return state.systemInfo.screenHeight
		}
	},
});

// 读取本地存储的状态
const savedState = uni.getStorageSync('vuex_state');
if (savedState) {
  store.replaceState(Object.assign(store.state, savedState));
}

// 监听状态变化并保存到本地存储
store.subscribe((mutation, state) => {
  uni.setStorageSync('vuex_state', state);
});

export default store;